<template>
  <div class="min-h-screen bg-base-100">
    <!-- 顶部导航栏 -->
    <div class="navbar bg-base-200 shadow-lg">
      <div class="flex-1">
        <a class="btn btn-ghost normal-case text-xl">个人中心</a>
      </div>
      <div class="flex-none gap-2">
        <div class="dropdown dropdown-end">
          <label tabindex="0" class="btn btn-ghost btn-circle avatar">
            <div class="w-10 rounded-full">
              <img :src="user.avatar" alt="用户头像" />
            </div>
          </label>
          <ul tabindex="0" class="mt-3 z-[1] p-2 shadow menu menu-sm dropdown-content bg-base-200 rounded-box w-52">
            <li><a @click="activeTab = 'profile'">个人资料</a></li>
            <li><a @click="activeTab = 'settings'">账户设置</a></li>
            <li><a @click="logout">退出登录</a></li>
          </ul>
        </div>
      </div>
    </div>

    <div class="container mx-auto px-4 py-8">
      <div class="grid grid-cols-1 lg:grid-cols-4 gap-6">
        <!-- 侧边栏 -->
        <div class="lg:col-span-1">
          <div class="card bg-base-200 shadow-xl">
            <div class="card-body items-center text-center">
              <div class="avatar">
                <div class="w-24 rounded-full ring ring-primary ring-offset-base-100 ring-offset-2">
                  <img :src="user.avatar" alt="用户头像" />
                </div>
              </div>
              <h2 class="card-title mt-4">{{ user.name }}</h2>
              <p class="text-sm opacity-70">{{ user.email }}</p>
              <div class="mt-2">
                <div class="badge badge-primary mr-2">{{ user.role }}</div>
                <div class="badge badge-outline">{{ user.memberSince }}</div>
              </div>
              <div class="divider"></div>
              <div class="stats stats-vertical shadow w-full">
                <div class="stat">
                  <div class="stat-title">积分</div>
                  <div class="stat-value">{{ user.points || 0 }}</div>
                </div>
                <div class="stat">
                  <div class="stat-title">等级</div>
                  <div class="stat-value">{{ user.level }}</div>
                </div>
              </div>
              <div class="divider"></div>
              <div class="w-full">
                <ul class="menu bg-base-200 w-full rounded-box">
                  <li>
                    <a 
                      @click="activeTab = 'profile'" 
                      :class="{ 'active': activeTab === 'profile' }"
                    >
                      <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
                      </svg>
                      个人资料
                    </a>
                  </li>
                  <li>
                    <a 
                      @click="activeTab = 'settings'" 
                      :class="{ 'active': activeTab === 'settings' }"
                    >
                      <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                      </svg>
                      账户设置
                    </a>
                  </li>
                  <li>
                    <a 
                      @click="activeTab = 'statistics'" 
                      :class="{ 'active': activeTab === 'statistics' }"
                    >
                      <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z" />
                      </svg>
                      数据统计
                    </a>
                  </li>
                  <li>
                    <a 
                      @click="activeTab = 'security'" 
                      :class="{ 'active': activeTab === 'security' }"
                    >
                      <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z" />
                      </svg>
                      安全中心
                    </a>
                  </li>
                </ul>
              </div>
            </div>
          </div>
        </div>

        <!-- 主内容区域 -->
        <div class="lg:col-span-3">
          <!-- 个人资料 -->
          <div v-if="activeTab === 'profile'" class="card bg-base-200 shadow-xl">
            <div class="card-body">
              <h2 class="card-title text-2xl mb-6">个人资料</h2>
              
              <div class="form-control w-full">
                <label class="label">
                  <span class="label-text">用户名</span>
                </label>
                <input type="text" v-model="user.name" class="input input-bordered w-full" />
              </div>
              
              <div class="form-control w-full mt-4">
                <label class="label">
                  <span class="label-text">电子邮箱</span>
                </label>
                <input type="email" v-model="user.email" class="input input-bordered w-full" />
              </div>
              
              <div class="form-control w-full mt-4">
                <label class="label">
                  <span class="label-text">手机号码</span>
                </label>
                <input type="tel" v-model="user.mobile_phone" class="input input-bordered w-full" />
              </div>
              
              <div class="form-control w-full mt-4">
                <label class="label">
                  <span class="label-text">个人简介</span>
                </label>
                <textarea v-model="user.description" class="textarea textarea-bordered h-24"></textarea>
              </div>
              
              <div class="form-control w-full mt-4">
                <label class="label">
                  <span class="label-text">头像</span>
                </label>
                <div class="flex items-center gap-4">
                  <div class="avatar">
                    <div class="w-16 rounded-full">
                      <img :src="user.avatar" alt="用户头像" />
                    </div>
                  </div>
                  <button class="btn btn-outline">更换头像</button>
                </div>
              </div>
              
              <div class="card-actions justify-end mt-6">
                <button class="btn btn-primary" @click="saveProfile">保存更改</button>
              </div>
            </div>
          </div>

          <!-- 账户设置 -->
          <div v-if="activeTab === 'settings'" class="card bg-base-200 shadow-xl">
            <div class="card-body">
              <h2 class="card-title text-2xl mb-6">账户设置</h2>
              
              <div class="collapse collapse-arrow bg-base-100 mb-4">
                <input type="checkbox" /> 
                <div class="collapse-title text-xl font-medium">
                  修改密码
                </div>
                <div class="collapse-content"> 
                  <div class="form-control w-full">
                    <label class="label">
                      <span class="label-text">当前密码</span>
                    </label>
                    <input type="password" v-model="passwords.current" class="input input-bordered w-full" />
                  </div>
                  
                  <div class="form-control w-full mt-4">
                    <label class="label">
                      <span class="label-text">新密码</span>
                    </label>
                    <input type="password" v-model="passwords.new" class="input input-bordered w-full" />
                  </div>
                  
                  <div class="form-control w-full mt-4">
                    <label class="label">
                      <span class="label-text">确认新密码</span>
                    </label>
                    <input type="password" v-model="passwords.confirm" class="input input-bordered w-full" />
                  </div>
                  
                  <div class="mt-4">
                    <button class="btn btn-primary" @click="changePassword">更新密码</button>
                  </div>
                </div>
              </div>
              
              <div class="collapse collapse-arrow bg-base-100 mb-4">
                <input type="checkbox" /> 
                <div class="collapse-title text-xl font-medium">
                  通知设置
                </div>
                <div class="collapse-content"> 
                  <div class="form-control">
                    <label class="label cursor-pointer">
                      <span class="label-text">电子邮件通知</span> 
                      <input type="checkbox" v-model="settings.emailNotifications" class="toggle toggle-primary" />
                    </label>
                  </div>
                  
                  <div class="form-control mt-2">
                    <label class="label cursor-pointer">
                      <span class="label-text">短信通知</span> 
                      <input type="checkbox" v-model="settings.smsNotifications" class="toggle toggle-primary" />
                    </label>
                  </div>
                  
                  <div class="form-control mt-2">
                    <label class="label cursor-pointer">
                      <span class="label-text">推送通知</span> 
                      <input type="checkbox" v-model="settings.pushNotifications" class="toggle toggle-primary" />
                    </label>
                  </div>
                </div>
              </div>
              
              <div class="collapse collapse-arrow bg-base-100 mb-4">
                <input type="checkbox" /> 
                <div class="collapse-title text-xl font-medium">
                  隐私设置
                </div>
                <div class="collapse-content"> 
                  <div class="form-control">
                    <label class="label cursor-pointer">
                      <span class="label-text">公开个人资料</span> 
                      <input type="checkbox" v-model="settings.publicProfile" class="toggle toggle-primary" />
                    </label>
                  </div>
                  
                  <div class="form-control mt-2">
                    <label class="label cursor-pointer">
                      <span class="label-text">显示在线状态</span> 
                      <input type="checkbox" v-model="settings.showOnlineStatus" class="toggle toggle-primary" />
                    </label>
                  </div>
                  
                  <div class="form-control mt-2">
                    <label class="label cursor-pointer">
                      <span class="label-text">允许数据分析</span> 
                      <input type="checkbox" v-model="settings.allowDataAnalytics" class="toggle toggle-primary" />
                    </label>
                  </div>
                </div>
              </div>
              
              <div class="collapse collapse-arrow bg-base-100">
                <input type="checkbox" /> 
                <div class="collapse-title text-xl font-medium">
                  主题设置
                </div>
                <div class="collapse-content"> 
                  <div class="form-control w-full">
                    <label class="label">
                      <span class="label-text">选择主题</span>
                    </label>
                    <select class="select select-bordered w-full" v-model="settings.theme">
                      <option value="light">浅色</option>
                      <option value="dark">深色</option>
                      <option value="cupcake">Cupcake</option>
                      <option value="bumblebee">Bumblebee</option>
                      <option value="emerald">Emerald</option>
                      <option value="corporate">Corporate</option>
                      <option value="synthwave">Synthwave</option>
                      <option value="retro">Retro</option>
                      <option value="cyberpunk">Cyberpunk</option>
                      <option value="valentine">Valentine</option>
                      <option value="halloween">Halloween</option>
                      <option value="garden">Garden</option>
                      <option value="forest">Forest</option>
                      <option value="aqua">Aqua</option>
                      <option value="lofi">Lofi</option>
                      <option value="pastel">Pastel</option>
                      <option value="fantasy">Fantasy</option>
                      <option value="wireframe">Wireframe</option>
                      <option value="black">Black</option>
                      <option value="luxury">Luxury</option>
                      <option value="dracula">Dracula</option>
                      <option value="cmyk">CMYK</option>
                      <option value="autumn">Autumn</option>
                      <option value="business">Business</option>
                      <option value="acid">Acid</option>
                      <option value="lemonade">Lemonade</option>
                      <option value="night">Night</option>
                      <option value="coffee">Coffee</option>
                      <option value="winter">Winter</option>
                    </select>
                  </div>
                  
                  <div class="mt-4">
                    <button class="btn btn-primary" @click="applyTheme">应用主题</button>
                  </div>
                </div>
              </div>
              
              <div class="card-actions justify-end mt-6">
                <button class="btn btn-primary" @click="saveSettings">保存设置</button>
              </div>
            </div>
          </div>

          <!-- 数据统计 -->
          <div v-if="activeTab === 'statistics'" class="card bg-base-200 shadow-xl">
            <div class="card-body">
              <h2 class="card-title text-2xl mb-6">数据统计</h2>
              
              <div class="stats shadow w-full">
                <div class="stat">
                  <div class="stat-figure text-primary">
                    <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="inline-block w-8 h-8 stroke-current">
                      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"></path>
                    </svg>
                  </div>
                  <div class="stat-title">收藏数</div>
                  <div class="stat-value text-primary">{{ statistics.favorites }}</div>
                  <div class="stat-desc">↗︎ 45% (30天)</div>
                </div>
                
                <div class="stat">
                  <div class="stat-figure text-secondary">
                    <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="inline-block w-8 h-8 stroke-current">
                      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"></path>
                    </svg>
                  </div>
                  <div class="stat-title">活跃度</div>
                  <div class="stat-value text-secondary">{{ statistics.activity }}</div>
                  <div class="stat-desc">↗︎ 21% (30天)</div>
                </div>
                
                <div class="stat">
                  <div class="stat-figure text-secondary">
                    <div class="avatar">
                      <div class="w-16 rounded-full">
                        <img src="https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg" />
                      </div>
                    </div>
                  </div>
                  <div class="stat-title">任务完成</div>
                  <div class="stat-value">{{ statistics.tasksCompleted }}</div>
                  <div class="stat-desc text-secondary">↗︎ 14% (30天)</div>
                </div>
              </div>
              
              <div class="divider">活动记录</div>
              
              <div class="overflow-x-auto">
                <table class="table">
                  <thead>
                    <tr>
                      <th>日期</th>
                      <th>活动</th>
                      <th>状态</th>
                      <th>积分</th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr v-for="(activity, index) in activities" :key="index">
                      <td>{{ activity.date }}</td>
                      <td>{{ activity.name }}</td>
                      <td>
                        <div class="badge" :class="getBadgeClass(activity.status)">
                          {{ activity.status }}
                        </div>
                      </td>
                      <td>{{ activity.points }}</td>
                    </tr>
                  </tbody>
                </table>
              </div>
              
              <div class="card-actions justify-center mt-6">
                <button class="btn btn-outline">查看更多</button>
              </div>
            </div>
          </div>

          <!-- 安全中心 -->
          <div v-if="activeTab === 'security'" class="card bg-base-200 shadow-xl">
            <div class="card-body">
              <h2 class="card-title text-2xl mb-6">安全中心</h2>
              
              <div class="alert shadow-lg mb-6">
                <div>
                  <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="stroke-info flex-shrink-0 w-6 h-6">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                  </svg>
                  <div>
                    <h3 class="font-bold">安全提示</h3>
                    <div class="text-xs">定期更改密码并启用双重认证可以提高账户安全性</div>
                  </div>
                </div>
                <button class="btn btn-sm">查看</button>
              </div>
              
              <div class="form-control">
                <label class="label cursor-pointer">
                  <span class="label-text">双重认证</span> 
                  <input type="checkbox" v-model="security.twoFactorAuth" class="toggle toggle-success" />
                </label>
                <div class="text-xs opacity-70 mt-1" v-if="!security.twoFactorAuth">
                  启用双重认证可以提高账户安全性，强烈建议开启
                </div>
                <div class="text-xs text-success mt-1" v-else>
                  您已开启双重认证，账户安全性得到提升
                </div>
              </div>
              
              <div class="divider">登录设备</div>
              
              <div class="overflow-x-auto">
                <table class="table">
                  <thead>
                    <tr>
                      <th>设备</th>
                      <th>位置</th>
                      <th>最后登录时间</th>
                      <th>操作</th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr v-for="(device, index) in security.devices" :key="index">
                      <td>
                        <div class="flex items-center space-x-3">
                          <div class="avatar">
                            <div class="mask mask-squircle w-12 h-12">
                              <img :src="getDeviceIcon(device.type)" alt="设备图标" />
                            </div>
                          </div>
                          <div>
                            <div class="font-bold">{{ device.name }}</div>
                            <div class="text-sm opacity-50">{{ device.type }}</div>
                          </div>
                        </div>
                      </td>
                      <td>{{ device.location }}</td>
                      <td>{{ device.lastLogin }}</td>
                      <td>
                        <button class="btn btn-xs btn-error" v-if="!device.current">
                          退出
                        </button>
                        <div class="badge badge-primary" v-else>当前设备</div>
                      </td>
                    </tr>
                  </tbody>
                </table>
              </div>
              
              <div class="divider">账户安全日志</div>
              
              <div class="overflow-x-auto">
                <table class="table">
                  <thead>
                    <tr>
                      <th>时间</th>
                      <th>活动</th>
                      <th>IP地址</th>
                      <th>状态</th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr v-for="(log, index) in security.logs" :key="index">
                      <td>{{ log.time }}</td>
                      <td>{{ log.activity }}</td>
                      <td>{{ log.ip }}</td>
                      <td>
                        <div class="badge" :class="log.status === '成功' ? 'badge-success' : 'badge-error'">
                          {{ log.status }}
                        </div>
                      </td>
                    </tr>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { useUserStore } from '@/stores/user'

export default {
  name: 'HomePage',
  data() {
    const userStore = useUserStore();
    return {
      // 当前激活的标签页
      activeTab: 'profile',
      userStore: userStore,
      // 用户信息
      user: userStore.user,
      // 密码修改
      passwords: {
        current: '',
        new: '',
        confirm: ''
      },
      // 账户设置
      settings: {
        emailNotifications: true,
        smsNotifications: false,
        pushNotifications: true,
        publicProfile: true,
        showOnlineStatus: true,
        allowDataAnalytics: false,
        theme: 'light'
      },
      // 统计数据
      statistics: {
        favorites: 256,
        activity: 89,
        tasksCompleted: 42
      },
      // 活动记录
      activities: [
        { date: '2023-10-15', name: '完成每日任务', status: '已完成', points: 30 },
        { date: '2023-10-14', name: '参与社区讨论', status: '已完成', points: 15 },
        { date: '2023-10-13', name: '上传资源', status: '审核中', points: 0 },
        { date: '2023-10-12', name: '完成每日任务', status: '已完成', points: 30 },
        { date: '2023-10-11', name: '邀请好友', status: '已完成', points: 50 }
      ],
      // 安全中心
      security: {
        twoFactorAuth: false,
        devices: [
          { 
            name: 'MacBook Pro', 
            type: 'Mac OS', 
            location: '北京', 
            lastLogin: '2023-10-15 14:30', 
            current: true 
          },
          { 
            name: 'iPhone 13', 
            type: 'iOS', 
            location: '北京', 
            lastLogin: '2023-10-14 09:15', 
            current: false 
          },
          { 
            name: 'Chrome', 
            type: 'Windows', 
            location: '上海', 
            lastLogin: '2023-10-10 18:45', 
            current: false 
          }
        ],
        logs: [
          { time: '2023-10-15 14:30', activity: '登录', ip: '192.168.1.1', status: '成功' },
          { time: '2023-10-14 09:15', activity: '登录', ip: '192.168.1.2', status: '成功' },
          { time: '2023-10-13 22:45', activity: '修改密码', ip: '192.168.1.1', status: '成功' },
          { time: '2023-10-12 16:30', activity: '登录', ip: '192.168.1.3', status: '失败' },
          { time: '2023-10-12 16:32', activity: '登录', ip: '192.168.1.3', status: '成功' }
        ]
      }
    }
  },
  methods: {
    // 获取设备图标
    getDeviceIcon(type) {
      const icons = {
        'Mac OS': 'https://cdn-icons-png.flaticon.com/512/2/2235.png',
        'iOS': 'https://cdn-icons-png.flaticon.com/512/831/831276.png',
        'Windows': 'https://cdn-icons-png.flaticon.com/512/888/888882.png',
        'Android': 'https://cdn-icons-png.flaticon.com/512/888/888839.png'
      };
      
      return icons[type] || 'https://cdn-icons-png.flaticon.com/512/1055/1055687.png';
    },
    // 获取状态徽章样式
    getBadgeClass(status) {
      switch (status) {
        case '已完成':
          return 'badge-success';
        case '审核中':
          return 'badge-warning';
        case '未完成':
          return 'badge-error';
        default:
          return 'badge-info';
      }
    },
    // 应用主题
    applyTheme() {
      document.documentElement.setAttribute('data-theme', this.settings.theme);
      alert(`已应用${this.settings.theme}主题`);
    },
    // 退出登录
    async logout() {
      await this.userStore.logout();
      this.$router.push('/guest/login');
    },
    // 保存个人资料
    saveProfile() {
      // 保存个人资料的逻辑
      alert('个人资料已保存');
    },
    // 修改密码
    changePassword() {
      // 修改密码的逻辑
      alert('密码修改功能待实现');
    },
    // 保存设置
    saveSettings() {
      // 保存设置的逻辑
      alert('设置已保存');
    }
  },
  mounted() {
    // 组件挂载后的逻辑
  }
}
</script>

